/*
 * Created on Feb 25, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntryList;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry;
import com.scalar.chiptrack.finance.openpo.ReceivingLineItem;
import com.scalar.chiptrack.finance.openpo.ReceivingLineItemList;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;

/**
 * @author Ravikanth
**/
public class RecevingLineItemLotDataEntryAddCommand extends ChipTrackExtendedAction
{
	public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception	
	{
		HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 		= "recevingLineItemLotEntry";
		HttpSession mSession 		= request.getSession();

		PurchaseOrderReceiptForm receiptForm = ( PurchaseOrderReceiptForm ) form;
		ReceivingLineItem recevingLineItem;
		ReceivingLineItemList receivingLineItemList;
		PurchaseOrderLineItemLotDataEntryList purchaseOrderLineItemLotDataEntryList;

		int recevingLineItemNumber = -1;
		Iterator lineItemIterator;
        boolean fromOpenReceivingLineitemLotEntry = Boolean.valueOf(request.getParameter( "openReceivingLineitemLotEntry" )).booleanValue();
		try
		{
            try
            {
                recevingLineItemNumber 	= Integer.parseInt( request.getParameter("purchaseOrderLineItemNumber") );
            }
            catch ( NumberFormatException e ) {}
            receivingLineItemList 	= receiptForm.getReceivingLineItemList();
			lineItemIterator 		= receivingLineItemList.iterator();
            if ( !fromOpenReceivingLineitemLotEntry )
            {
                //for assigning New Purchase Order Line Item Data Entry to the selected Purchase Order Line Item, if it's is empty.
                while ( lineItemIterator.hasNext() )
                {
                    recevingLineItem = ( ReceivingLineItem ) lineItemIterator.next();
                    PurchaseOrderLineItemLotDataEntry purchaseOrderLineItemLotDataEntry = null;
                    // If Selected Line item index and Line Item Index match then set values of Purchasing Lot Number List.
                    if ( recevingLineItem.getPurchaseOrderLineItemNumber() == recevingLineItemNumber )
                    {
                        purchaseOrderLineItemLotDataEntryList = recevingLineItem.getPurchaseOrderLineItemLotDataEntryList();
                        purchaseOrderLineItemLotDataEntry = new PurchaseOrderLineItemLotDataEntry();
                        purchaseOrderLineItemLotDataEntry.setSelected( true );
                        purchaseOrderLineItemLotDataEntry.setProcessStepId( recevingLineItem.getProcessStepId() );
                        purchaseOrderLineItemLotDataEntry.setPartNumber( recevingLineItem.getPartNumber() );
                        purchaseOrderLineItemLotDataEntry.setPoNumber( receiptForm.getPurchaseOrderNumber() );
                        purchaseOrderLineItemLotDataEntry.setPsTypeName( recevingLineItem.getProcessName() );
                        purchaseOrderLineItemLotDataEntryList.add( purchaseOrderLineItemLotDataEntry );
                        //recevingLineItem.setPurchaseOrderLineItemLotDataEntryList( purchaseOrderLineItemLotDataEntryList );
                        if ( receiptForm.getPoType() == Constants.PO_TYPE_OPEN )
                        {
                            if ( purchaseOrderLineItemLotDataEntry == null )
                            {
                                purchaseOrderLineItemLotDataEntry = new PurchaseOrderLineItemLotDataEntry();
                                purchaseOrderLineItemLotDataEntry.setSelected( true );
                                purchaseOrderLineItemLotDataEntry.setPoNumber( receiptForm.getPurchaseOrderNumber() );
                            }
                            PurchaseOrderLineItemLotDataEntryList openPOLotList = receiptForm.getOpenPOLotList();
                            openPOLotList.add( purchaseOrderLineItemLotDataEntry );
                        }
                    }
                }
                actionForward = "recevingLineItemLotEntry";
            }
            else
            {
                PurchaseOrderLineItemLotDataEntry purchaseOrderLineItemLotDataEntry = new PurchaseOrderLineItemLotDataEntry();
                purchaseOrderLineItemLotDataEntry.setSelected( true );
                purchaseOrderLineItemLotDataEntry.setPoNumber( receiptForm.getPurchaseOrderNumber() );
                PurchaseOrderLineItemLotDataEntryList openPOLotList = receiptForm.getOpenPOLotList();
                openPOLotList.add( purchaseOrderLineItemLotDataEntry );
                request.setAttribute( "processNameList", PurchaseOrderDAO.loadProcessNames( receiptForm.getVendor_id() ) );
                actionForward = "openRecevingLineItemLotEntry";
            }

		}
		catch ( Exception e )
		{
            e.printStackTrace();
            Logger.error( ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERRROR_LOADING_PURCHASE_ORDER_LINE_ITEM_PURCHASING_LOT_DATA_ENTRY, e ) );
			actionForward = (fromOpenReceivingLineitemLotEntry) ? "openRecevingLineItemLotEntry" : "recevingLineItemLotEntry" ;
		}

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}
}
